package com.sky.controller.admin;


import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setmealList", key = "#setmealDTO.categoryId")//新增套餐只涉及某个分类，所以只需要删除相应分类下的缓存就可以了
    public Result addDish(@RequestBody SetmealDTO setmealDTO) {
        log.info("添加新套餐:{}", setmealDTO);
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 分页展示套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("分页展示套餐")
    @GetMapping("/page")
    public Result<PageResult> getDishList(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("菜品分页查询：{}",setmealPageQueryDTO);
        PageResult pageResult = setmealService.getSetmeal(setmealPageQueryDTO);
        log.info(pageResult.toString());
        return Result.success(pageResult);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id){
        SetmealVO setmealVO = setmealService.getSetmealById(id);
        log.info("根据id查询套餐:{}",setmealVO.toString());
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealList", allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("获取修改的套餐信息:{}",setmealDTO);
        setmealService.updataSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 删除套餐及套餐里面的菜品
     * @param str
     * @return
     */
    @ApiOperation("删除套餐及套餐里面的菜品")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealList", allEntries = true)
    public Result deleteSetmeal(@RequestParam("ids") String str){
        String[] ids = str.split(",");
        log.info("删除ids:{}", Arrays.toString(ids));
        setmealService.delectSetmeal(ids);
        return Result.success();
    }

    /**
     * 修改套餐的状态
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("修改套餐的状态")
    @PostMapping("status/{status}")
    @CacheEvict(cacheNames = "setmealList", allEntries = true)
    public Result updateSetmealStatus(@PathVariable("status") Integer status,Long id) {
        log.info("修改套餐{}的状态为{}",id,status);
        SetmealDTO setmealDTO = new SetmealDTO();
        setmealDTO.setId(id);
        setmealDTO.setStatus(status);
        setmealService.updataSetmeal(setmealDTO);
        return Result.success();
    }

}
